using System;
using System.Configuration;
using System.Web.Services.Protocols;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using HelpdeskServices.CrmWebReference;
using HelpdeskServices.Properties;

public partial class Setup_WorkFlowSetup : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillDropDownControlsWithAvailibleWorkflows();
            queueList();
            try
            {
                loadCurrentWorkflows();
                subjectList();
            }
            catch (Exception unexpectedError)
            {
                Response.Write("Something is not write in the config file, remove some of the guid's and try again, reboot or iis reset wont help for this, more info: "+ unexpectedError.Message);
            }
        }
    }

    /// <summary>
    /// Works the flow list.
    /// </summary>
    private void FillDropDownControlsWithAvailibleWorkflows()
    {
        CrmService localService = Common.CrmWebServiceProxy();

        string xmlResults = "";
        try
        {
            xmlResults = localService.Fetch(Resources.XmlFetchWorkFlowProcessList);
        }
        catch (SoapException soapError)
        {
            Response.Write(soapError.Detail.InnerXml);
        }
        XmlDocument workFlowProcessList = new XmlDocument();
        workFlowProcessList.LoadXml(xmlResults);

        foreach (XmlNode node in workFlowProcessList.ChildNodes[0].ChildNodes)
        {
            DropDownList1.Items.Add(
                new ListItem(node.SelectSingleNode("name").InnerText, node.SelectSingleNode("processid").InnerText));
            DropDownList2.Items.Add(
                new ListItem(node.SelectSingleNode("name").InnerText, node.SelectSingleNode("processid").InnerText));
        }
    }

    private void queueList()
    {
        CrmService localService = Common.CrmWebServiceProxy();

        string xmlResults = localService.Fetch(Resources.XmlFetchQueueList);
        XmlDocument queueDoc = new XmlDocument();
        queueDoc.LoadXml(xmlResults);
        foreach (XmlNode node in queueDoc.ChildNodes[0].ChildNodes)
        {
            supportQueueSelection.Items.Add(
                new ListItem(node.SelectSingleNode("name").InnerText, node.SelectSingleNode("queueid").InnerText));
        }
    }

    private void loadCurrentWorkflows()
    {
        if(ConfigurationManager.AppSettings[Resources.ConfigurationKeyForWorkFlow1Id].ToString().Length > 32)
        {
            Guid workflow1 = new Guid(ConfigurationManager.AppSettings[Resources.ConfigurationKeyForWorkFlow1Id].ToString());
            Guid workflow2 = new Guid(ConfigurationManager.AppSettings[Resources.ConfigurationKeyForWorkFlow2Id].ToString());

            TargetRetrieveWFProcess target1 = new TargetRetrieveWFProcess();
            target1.EntityId = workflow1;

            TargetRetrieveWFProcess target2 = new TargetRetrieveWFProcess();
            target2.EntityId = workflow2;

            wfprocess workFlowProcess1 = (wfprocess)loadWorkFlow(target1).BusinessEntity;
            if (workFlowProcess1 != null)
            {
                CurrentWorkFlowLabel1.Text = workFlowProcess1.name;
                CurrentWorkFlowLabel1.ToolTip = workFlowProcess1.description;
            }

            wfprocess workFlowProcess2 = (wfprocess)loadWorkFlow(target2).BusinessEntity;
            if (workFlowProcess2 != null)
            {
                CurrentWorkFlowLabel2.Text = workFlowProcess2.name;
                CurrentWorkFlowLabel2.ToolTip = workFlowProcess2.description;
            }
        }
        else
        {
            Response.Write("No guid's in configuration setting");
        }
    }

    private static RetrieveResponse loadWorkFlow(TargetRetrieveWFProcess workFlow)
    {
        CrmService localCrmService = Common.CrmWebServiceProxy();
        RetrieveRequest retrieve = new RetrieveRequest();
        retrieve.Target = workFlow;
        retrieve.ColumnSet = new AllColumns();
        RetrieveResponse serverResponse = (RetrieveResponse) localCrmService.Execute(retrieve);
        return serverResponse;
    }

    protected void UpdateWorkFlow1Button_Click(object sender, EventArgs e)
    {
        Common.UpdateConfigurationFile("WorkFlow1Id", DropDownList1.SelectedValue.ToString());
        Response.Write("Set successfull to:"+DropDownList1.SelectedValue.ToString());
    }

    protected void UpdateWorkFlow2Button_Click(object sender, EventArgs e)
    {
        Common.UpdateConfigurationFile("WorkFlow2Id", DropDownList2.SelectedValue.ToString());
        Response.Write("Set successfull set to:"+DropDownList2.SelectedValue.ToString());
    }

    protected void subjectList()
    {
        CrmService localService = Common.CrmWebServiceProxy();

        string xmlResults = localService.Fetch(Resources.XmlFetchSubjects);
        XmlDocument queueDoc = new XmlDocument();
        queueDoc.LoadXml(xmlResults);
        foreach (XmlNode node in queueDoc.ChildNodes[0].ChildNodes)
        {
            EmailSubjectList.Items.Add(
                new ListItem(node.SelectSingleNode("title").InnerText, node.SelectSingleNode("subjectid").InnerText));
            webImportSubjectList.Items.Add(
                new ListItem(node.SelectSingleNode("title").InnerText, node.SelectSingleNode("subjectid").InnerText));
        }
    }


    protected void updateSupportQueueButton_Click(object sender, EventArgs e)
    {
        Common.UpdateConfigurationFile("SupportQueueId", supportQueueSelection.SelectedValue.ToString());
       Response.Write("Set successfull set to:"+supportQueueSelection.SelectedValue.ToString());
    }

    protected void updateEmailSubjectId_Click(object sender, EventArgs e)
    {
        Common.UpdateConfigurationFile("ConvertEmailSubjectId",EmailSubjectList.SelectedValue.ToString());
        Response.Write("set ConvertEmailSubjectId="+EmailSubjectList.SelectedValue.ToString());
    }

    protected void updateWebSubjectList_Click(object sender, EventArgs e)
    {
        Common.UpdateConfigurationFile("ImportWebCaseSubjectId",webImportSubjectList.SelectedValue.ToString());
        Response.Write("set ImportWebCaseSubjectId=" + webImportSubjectList.SelectedValue.ToString());
    }

}